﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Collections.Specialized;
using System.Security.Cryptography.X509Certificates;
using System.IO;

namespace DataAccess
{
    public partial class DataAccess
    {
        public DataAccessResult DeleteDeployment(String SubscriptionID, String CertificatePath, String ServiceName, String DeploymentSlot)
        {
            DataAccessResult result = new DataAccessResult();
            string requestUrl = "https://management.core.windows.net/" + SubscriptionID + "/services/hostedservices/" + ServiceName + "/deploymentslots/" + DeploymentSlot;
            string ReturnBody = string.Empty;

            WebResponse resp = null;
            try
            {
                NameValueCollection RequestHeaders = new NameValueCollection();
                RequestHeaders.Add("x-ms-version", x_ms_version);

                X509Certificate cert = X509Certificate.CreateFromCertFile(CertificatePath);
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(requestUrl);
                request.Method = "DELETE";
                request.ClientCertificates.Add(cert);
                if (RequestHeaders != null)
                {
                    request.Headers.Add(RequestHeaders);
                }


                resp = request.GetResponse();
                // RequestID = resp.Headers["x-ms-request-id"];


                if (resp != null)
                {

                    using (StreamReader sr = new StreamReader(resp.GetResponseStream(), true))
                    {
                        ReturnBody = sr.ReadToEnd();
                        sr.Close();
                    }
                    if (ReturnBody != null)
                    {


                        HttpWebResponse httpResp = (HttpWebResponse)resp;
                        result.IsOperationSuccessful = httpResp.StatusCode.Equals(HttpStatusCode.Accepted);

                    }


                }
            }
            catch (WebException webEx)
            {
                result.Error = webEx;
                result.IsOperationSuccessful = false;
            }
            catch (Exception excep)
            {
                result.Error = excep;
                result.IsOperationSuccessful = false;
            }
            finally
            {
                //req = null;
                if (resp != null) { resp.Close(); }
                resp = null;
            }
            return result;

        }
    }
}
